package controller;

import cn.lovezsm.bjcjbase.bean.GridMap;
import cn.lovezsm.bjcjbase.util.SpringUtils;
import cn.lovezsm.bjcjbase.util.WebUtils;
import cn.lovezsm.bjcjbase.config.CollectionConfig;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;


@RestController
@RequestMapping("api.collection")
public class CollectionController {

    public static final String ERROR = "error";

    public File inFile;
    public File outFileName;


    {
        //"in_fingerprintdata.dat";
        try {
            inFile = File.createTempFile(WebUtils.UUIDGenerator.getUUID(),"in_fingerprintdata");
            outFileName = File.createTempFile(WebUtils.UUIDGenerator.getUUID(),"out_fingerprintdata");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @PostMapping("submitGridFile")
    public boolean submitGridFile(@RequestParam("file") MultipartFile file){
        CollectionConfig bean = SpringUtils.getBean(CollectionConfig.class);
        try {
            File tmp = WebUtils.transMultipartFile(file);
            GridMap gridMap = GridMap.buildByFile(tmp, "");
            bean.setGridMap(gridMap);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
//        return FileUtils.getFileNameNoEx(file.getOriginalFilename());
    }

    @PostMapping("submitOpen")
    public void submitOpen(boolean open){
        CollectionConfig bean = SpringUtils.getBean(CollectionConfig.class);
        bean.setOpen(open);
    }






}
